﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;
using System.Configuration;

namespace Prodma.DataAccess.DataService
{
    public class AgencyService : IAgencyRepository
    {

        private DataContext context = new DataContext();


        #region IRecordOperation<tb_Agency,AgencyVm> Members

        public IList<AgencyVm> GetList()
        {
            throw new NotImplementedException();
        }

        public void New(tb_Agency entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Agency(entity);
                context.SaveChanges();
            }
        }
        public AgencyVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from agency in context.tb_Agency
                            where agency.Id == id
                            select new AgencyVm()
                            {
                                Id = agency.Id,
                                Name = agency.Name,
                                City = new CityVm() { Id = agency.tb_City.Id, Name = agency.tb_City.Name },
                                Phone = agency.Phone,
                                Fax = agency.Fax,
                                Mail = agency.Mail,
                                Address = agency.Address,
                                State = new StateVm() { Id = agency.tb_State.Id }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Agency entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from agency in context.tb_Agency
                            where agency.Id == id
                            select agency).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Phone = entity.Phone;
                edit.Fax = entity.Fax;
                edit.Mail = entity.Mail;
                edit.Address = entity.Address;
                edit.tb_CityReference.EntityKey = entity.tb_CityReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from agency in context.tb_Agency
                              where agency.Id == id
                              select agency).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IPageOperation<AgencyVm> Members

        public IList<AgencyVm> GetList(int page)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from agency in context.tb_Agency
                            select new AgencyVm()
                            {
                                Id = agency.Id,
                                Name = agency.Name,
                                City = new CityVm() { Id = agency.tb_City.Id, Name = agency.tb_City.Name },
                                State = new StateVm() { Id = agency.tb_State.Id, Name = agency.tb_State.Name }
                            }).OrderBy(x => x.City.Name).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        #endregion

        #region IPageableFindOperation<AgencyVm> Members

        public IList<AgencyVm> GetList(int page, Dictionary<string, string> find)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from agency in context.tb_Agency
                            where agency.Name.StartsWith(findName)
                            select new AgencyVm()
                            {
                                Id = agency.Id,
                                Name = agency.Name,
                                City = new CityVm() { Id = agency.tb_City.Id, Name = agency.tb_City.Name },
                                State = new StateVm() { Id = agency.tb_State.Id, Name = agency.tb_State.Name }
                            }).OrderBy(x => x.City.Name).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        #endregion

        #region IRowCountOperation Members

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from agency in context.tb_Agency
                                select agency).Count();

                return rowCount;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from agency in context.tb_Agency
                                where agency.Name.StartsWith(findName)
                                select agency).Count();

                return rowCount;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from agency in context.tb_Agency
                                 where agency.Id == id
                                 select agency).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from agency in context.tb_Agency
                                 where agency.Id == id
                                 select agency).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

    }
}
